package tool

import (
	"context"
	"fmt"
	"reflect"
	model "service/model/crm_order"
	kbModel "service/model/kb_pro"

	gpb "gitee.com/cloudapex/protos/crm_order/pb"
	kbGpb "gitee.com/cloudapex/protos/pro_know/pb"
	"gitee.com/cloudapex/ulern/htp"
	"gitee.com/cloudapex/ulern/util"
	"github.com/gin-gonic/gin"
	//userModel  "service/model/crm_order"
)

func init() {

}

type Wx struct {
	OrderId int64 `form:"order_id"   json:"order_id" binding:"required"`
}

//调用微信支付
func (this *Wx) Handle(c *gin.Context, ctx context.Context) htp.Response {

	in := &gpb.CrmOrderGetReq{
		OrderId: this.OrderId,
	}
	res, err := model.Order.GetOrderInfo(ctx, in)
	if err != nil {
		return htp.RespModelErr("", err)
	}
	util.Debug("%s", res)
	fmt.Println(res)

	//判断订单详情是否存在
	if reflect.DeepEqual(res, gpb.CrmOrderCreateRes{}) || res.Status != 1 {
		return htp.RespErr(int(res.Status), "订单不存在或已支付", nil)
	}

	var wx WxPayServer
	wx.OrderNum = res.BillNo + "C"
	wx.Total = res.TotalPrice
	url, _ := wx.WxPayOrder(ctx)

	return htp.RespOK("", url)
}

type Lcw struct {
	OrderId int64 `form:"order_id"   json:"order_id" binding:"required"`
}

//调用扫呗支付
func (this *Lcw) Handle(c *gin.Context, ctx context.Context) htp.Response {

	in := &gpb.CrmOrderGetReq{
		OrderId: this.OrderId,
	}
	res, err := model.Order.GetOrderInfo(ctx, in)
	if err != nil {
		return htp.RespModelErr("", err)
	}
	util.Debug("%s", res)

	fmt.Println(res.Status)
	fmt.Println(reflect.DeepEqual(res, gpb.CrmOrderCreateRes{}))

	//判断订单详情是否存在
	if reflect.DeepEqual(res, gpb.CrmOrderCreateRes{}) || res.Status != 1 {
		return htp.RespErr(int(res.Status), "订单不存在或已支付", nil)
	}

	var lcw LcPayServer
	lcw.TerminalTrace = res.BillNo
	lcw.TotalFee = int64(res.TotalPrice)
	//lcw.PayType = this.PayType
	url, _ := lcw.LcPay(ctx)
	return htp.RespOK("", url)
}

type AliPay struct {
	OrderId int64 `form:"order_id"   json:"order_id" binding:"required"`
}

func (this *AliPay) Handle(c *gin.Context, ctx context.Context) htp.Response {

	in := &gpb.CrmOrderGetReq{
		OrderId: this.OrderId,
	}
	res, err := model.Order.GetOrderInfo(ctx, in)
	if err != nil {
		return htp.RespModelErr("", err)
	}
	util.Debug("%s", res)
	fmt.Println(res)

	//判断订单详情是否存在
	if reflect.DeepEqual(res, gpb.CrmOrderCreateRes{}) || res.Status != 1 {
		return htp.RespErr(int(res.Status), "订单不存在或已支付", nil)
	}
	var alipay AliPayServer
	alipay.OrderNum = res.BillNo + "C" //支付宝扫码渠道
	fmt.Println("订单表里的实付余额", res.TotalPrice)
	alipay.Total = float64(res.TotalPrice) / 100
	fmt.Println("订单心情数据", res)
	fmt.Println("支付宝需要的金额", alipay.Total)
	alipay.Subject = "宇学教育"
	qrCode, err := alipay.GoPay(ctx)
	if err != nil {
		return htp.RespErr(1, err.Error(), err)
	}
	return htp.RespOK("", qrCode)

}

type NowPays struct {
	OrderId int64 `form:"order_id"   json:"order_id" binding:"required"`
}

func (this *NowPays) Handle(c *gin.Context, ctx context.Context) htp.Response {

	in := &gpb.CrmOrderGetReq{
		OrderId: this.OrderId,
	}
	res, err := model.Order.GetOrderInfo(ctx, in)
	if err != nil {
		return htp.RespModelErr("", err)
	}
	util.Debug("%s", res)
	fmt.Println(res)
	//判断订单详情是否存在
	if reflect.DeepEqual(res, gpb.CrmOrderCreateRes{}) || res.Status != 1 {
		return htp.RespErr(int(res.Status), "订单不存在或已支付", nil)
	}
	var nowpay NowPayServer
	nowpay.MhtOrderNo = res.BillNo
	nowpay.MhtOrderAmt = int64(res.TotalPrice)

	payUrl, err := nowpay.NowPay(ctx)
	if err != nil {
		return htp.RespErr(1, err.Error(), err)
	}
	if payUrl == "" {
		return htp.RespErr(2, "支付已超时,换其他平台支付", nil)
	}
	return htp.RespOK("", payUrl)

}

type Test struct {
}

func (this *Test) Handle(c *gin.Context, ctx context.Context) htp.Response {

	//req := &hrGpb.LoginCacheReq{
	//	Id: 1,
	//}
	//userInfo, _ := hrModel.UserCacheServer.GetAllLoginCache(ctx, req)
	//util.Debug("%s",userInfo)

	//req1 := &gpb.ClassAgreeReq{
	//	ClassId: 240,
	//}
	//userInfo1, _ := model.UserClass.GetClassGetAgree(ctx, req1)
	//util.Debug("%s",userInfo1)

	var proclas kbGpb.ProClassReq
	var class kbGpb.Class
	var pro kbGpb.Project
	class.Id = 261
	proclas.Class = &class
	proclas.Pro = &pro
	prolist := kbGpb.ProListReq{ProClassReq: &proclas, Page: 1, Count: 1}
	rep, err := kbModel.ProClass.GetProClassList(ctx, &prolist)

	util.Debug("%s", rep.ProList[0].Class)
	util.Debug("%s", err)
	util.Debug("%s", &proclas)

	//ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	//defer cancel()
	//in := &gpb.CrmOrderGetReq{
	//	BillNo: "G100120201109000100",
	//}
	//res, _ := model.Order.GetOrderInfo(ctx, in)
	//util.Debug("%s",res)
	return htp.RespOK("", "")

}

/**
检查订单是否支付成功
*/
type Check struct {
	OrderId int64 `form:"order_id"   json:"order_id" binding:"required"`
}

func (this *Check) Handle(c *gin.Context, ctx context.Context) htp.Response {

	in := &gpb.CrmOrderGetReq{
		OrderId: this.OrderId,
	}
	res, err := model.Order.GetOrderInfo(ctx, in)
	if err != nil {
		return htp.RespModelErr("", err)
	}
	util.Debug("%s", res)
	fmt.Println(res)

	//判断订单详情是否存在
	if reflect.DeepEqual(res, gpb.CrmOrderCreateRes{}) {
		return htp.RespErr(int(res.Status), "订单不存在", nil)
	}
	var state bool
	if res.Status > 1 {
		state = true
	} else {
		state = false
	}
	return htp.RespOK("", state)

}

type Code struct {
	OrderId int64 `form:"order_id"   json:"order_id" binding:"required"`
	PayType int64 `form:"pay_type"   json:"pay_type" binding:"required"`
}

func (this *Code) Handle(c *gin.Context, ctx context.Context) htp.Response {

	in := &gpb.CrmOrderGetReq{
		OrderId: this.OrderId,
	}
	res, err := model.Order.GetOrderInfo(ctx, in)
	if err != nil {
		return htp.RespModelErr("", err)
	}
	util.Debug("%s", res)
	fmt.Println(res)
	//判断订单详情是否存在
	if reflect.DeepEqual(res, gpb.CrmOrderCreateRes{}) || res.Status != 1 {
		return htp.RespErr(int(res.Status), "订单不存在或已支付", nil)
	}

	var url string

	switch this.PayType {

	case 1:
		var nowpay NowPayServer
		nowpay.MhtOrderNo = res.BillNo
		nowpay.MhtOrderAmt = int64(res.TotalPrice)
		url, err = nowpay.NowPay(ctx)
		break
	case 2:
		var alipay AliPayServer
		alipay.OrderNum = res.BillNo + "C" //支付宝扫码渠道
		fmt.Println("订单表里的实付余额", res.TotalPrice)
		alipay.Total = float64(res.TotalPrice) / 100
		fmt.Println("订单心情数据", res)
		fmt.Println("支付宝需要的金额", alipay.Total)
		alipay.Subject = "宇学教育"
		url, err = alipay.GoPay(ctx)
		break
	case 3:
		var lcw LcPayServer
		lcw.TerminalTrace = res.BillNo
		lcw.TotalFee = int64(res.TotalPrice)
		url, err = lcw.LcPay(ctx)
		break
	case 4:
		var wx WxPayServer
		wx.OrderNum = res.BillNo + "C"
		wx.Total = res.TotalPrice
		url, err = wx.WxPayOrder(ctx)
		break
	default:
		var wx WxPayServer
		wx.OrderNum = res.BillNo + "C"
		wx.Total = res.TotalPrice
		url, err = wx.WxPayOrder(ctx)
		break
	}

	if err != nil {
		return htp.RespErr(1, err.Error(), err)
	}

	return htp.RespOK("", url)

}
